package com.ml.doctor.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.ml.doctor.R;
import com.ml.doctor.bean.ChatDataBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by kun on 17/12/18.
 */

public class ChartPop extends BasePopupWindow {
    LineChart lineChartView;
    TextView textView;

    public ChartPop(Context context) {
        super(context);
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_chat, null);
        setContentView(contentView);
        lineChartView = (LineChart) contentView.findViewById(R.id.linechat);
        textView = (TextView) contentView.findViewById(R.id.txt_title);
        contentView.findViewById(R.id.layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setData(List<ChatDataBean> list, int temp, String txtY, String txtX, String title) {
        initPointValues(temp, list);
    }

    /**
     * 体温图的设置
     */
    private void setChart(int min) {

        //x轴右下角文字描述
        lineChartView.getDescription().setEnabled(false);
        // enable touch gestures 启用触
        lineChartView.setTouchEnabled(true);

        //启用坐标轴是否可以上下拖动
        lineChartView.setDragEnabled(true);
        //启用缩放
        lineChartView.setScaleEnabled(true);
        //禁止y轴缩放
        lineChartView.setScaleYEnabled(false);
        lineChartView.setExtraLeftOffset(10);
        lineChartView.setExtraRightOffset(10);
        //20个数据以后不再显示注释标签
        lineChartView.setMaxVisibleValueCount(10);
        lineChartView.setNoDataText("暂无数据");
        XAxis xAxis = lineChartView.getXAxis();
        //绘制底部的X轴
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //启用X轴的网格虚线
        xAxis.setDrawGridLines(false);
        //缩放时候的粒度
        xAxis.setGranularity(1);
        xAxis.setTextSize(20f);
        //在可见范围只显示四个
        xAxis.setLabelCount(4);
        YAxis leftAxis = lineChartView.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.resetAxisMaximum();
        leftAxis.resetAxisMinimum();
        leftAxis.setAxisMinimum(min);
        leftAxis.setTextSize(20f);
        leftAxis.setDrawGridLines(false);//不启用y轴的参考线
        //启用零线
        leftAxis.setDrawZeroLine(false);

        //绘制警戒线在绘制数据之后
        leftAxis.setDrawLimitLinesBehindData(false);

        //禁用右边的Y轴
        lineChartView.getAxisRight().setEnabled(false);
        //动画时间
        lineChartView.animateX(1000);

    }

    private void setData(ArrayList<Entry> values, ArrayList<Entry> values2) {

        LineDataSet set1;
        LineDataSet set2 = null;
        set1 = new LineDataSet(values, "");
        set1.setDrawIcons(false);
        set1.enableDashedHighlightLine(10f, 0f, 0f);
        set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setValueTextSize(18f);
        set1.setColor(getContext().getResources().getColor(R.color.gray_line));
        set1.setCircleColor(getContext().getResources().getColor(R.color.main_color));
        set1.setLineWidth(2f);
        set1.setCircleRadius(4f);
        set1.setDrawCircleHole(true);
        set1.setCircleHoleRadius(4f);
        set1.setFormLineWidth(0f);
        set1.setFormLineDashEffect(new DashPathEffect(new float[]{0f, 0f}, 0f));
        set1.setFormSize(0f);
        set1.setDrawFilled(false);
        set1.setMode(LineDataSet.Mode.LINEAR);
        if (values2 != null) {
            set2 = new LineDataSet(values2, "");
            set2.setDrawIcons(false);
            set2.enableDashedHighlightLine(10f, 0f, 0f);
            set2.setHighLightColor(Color.rgb(244, 117, 117));
            set2.setValueTextSize(18f);
            set2.setColor(getContext().getResources().getColor(R.color.gray_line));
            set2.setCircleColor(getContext().getResources().getColor(R.color.main_color));
            set2.setLineWidth(2f);
            set2.setCircleRadius(4f);
            set2.setDrawCircleHole(true);
            set2.setCircleHoleRadius(4f);
            set2.setFormLineWidth(0f);
            set2.setFormLineDashEffect(new DashPathEffect(new float[]{0f, 0f}, 0f));
            set2.setFormSize(0f);
            set2.setDrawFilled(false);
            set2.setMode(LineDataSet.Mode.LINEAR);
        }

        ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
        dataSets.add(set1);
        if (set2 != null) {
            dataSets.add(set2);
        }
        LineData lineData = new LineData(dataSets);
        lineChartView.setData(lineData);
    }

    private void initPointValues(int temp, List<ChatDataBean> list) {
        ArrayList<Entry> values = new ArrayList<Entry>();
        ArrayList<Entry> values2 = new ArrayList<Entry>();
        switch (temp) {
            case 1:
                textView.setText("体温表");
                setChart(30);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).temper_ature));
//        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
                }
                setData(values, null);
                break;
            case 2:
                textView.setText("血压表");
                setChart(50);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).high_pressure));
//        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
                }
                for (int i = 0; i < list.size(); i++) {
                    values2.add(new Entry(i, list.get(i).high_pressure));
                }
                setData(values, values2);
                break;
            case 3:
                textView.setText("心率表");
                setChart(34);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).low_pressure));
//        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
                }
                setData(values, null);
                break;
            case 4:
                textView.setText("血糖表");
                setChart(0);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).blood_sugar));
//        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
                }
                setData(values, null);
                break;
            case 5:
                textView.setText("血氧表");
                setChart(90);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).blood_oxygen));
//        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
                }
                setData(values, null);
                break;
            case 6:
                textView.setText("脉搏表");
                setChart(30);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).pulse));
//        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
                }
                setData(values, null);
                break;
            case 7:
                textView.setText("胆固醇表");
                setChart(0);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).blood_sugar));
//        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
                }
                setData(values, null);
                break;
            case 8:
                textView.setText("尿酸表");
                setChart(70);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).blood_sugar));
//        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
                }
                setData(values, null);
                break;
        }
    }
}
